/*
 * @Author: Y先森
 * @Date: 2023-05-06 09:27:56
 * @LastEditors: y && 250048395@qq.com
 * @LastEditTime: 2023-05-06 10:08:02
 * @FilePath: \y-website\src\libs\confirm\index.js
 */
import { h, render} from 'vue'
import confirmComponent from './index.vue'

/**
 * 
 * @param {*} title 标题
 * @param {*} content 文本
 * @param {*} cancelText 取消按钮文本
 * @param {*} confirmText 确定按钮文本
 * @returns 
 */
export const confirm = (
    title,
    content,
    cancelText,
    confirmText,
)=> {
    return new Promise((resolve, reject) => {
        // 允许只传递 content
        if(title && !content) {
            content = title
            title = ''
        }

        // 关闭弹层事件
        const close = ()=> {
            render(null, document.body)
        }

        // 取消按钮事件
        const cancelHandler = ()=> {
            reject(new Error('取消按钮点击'))
        }

        // 确定按钮事件
        const confirmHandler = ()=> {
            resolve()
        }

        // 1.vnode
        const vnode = h(confirmComponent,{
            title,
            content,
            cancelText,
            confirmText,
            confirmHandler,
            cancelHandler,
            close
        })

        // 2.挂载body并渲染
        render(vnode,document.body)
    })
}